﻿//用户权限中角色管理

//edit by zman
var m_role = {};
//var m_enable = 0;
var m_curTabIdx = -1;
var m_arrFunc = null;
var m_arrUser = null;
//var m_creatorId = -1;

function RoleObject(id,name, note, creatorid, creator)
{
    this.id = id;
    this.name = name;
    this.note = note;
    this.creatorId = creatorid;
    this.creator = creator;
}

//window.onresize = initFrame;
window.onload = init;

function initFrame()
{
    var docWidth = document.documentElement.clientWidth;
    var docHeight = document.documentElement.clientHeight;
    var elemTab = $("role_tab");
    var elemBtn = $("role_btn");
    var elemMain = $("role_main");
    var vHeight = docHeight - elemTab.offsetHeight - elemBtn.offsetHeight;
    elemMain.cleanWhitespace();
    for (var i = 0; i < elemMain.childNodes.length; i++)
    {
        elemMain.childNodes[i].style.width = "100%";
        elemMain.childNodes[i].style.height = vHeight + "px";
    }
    var arrElem = document.getElementsByTagName("select");
    for (var i = 0; i < arrElem.length; i++)
    {
        arrElem[i].style.height = (vHeight - 25) + "px";
    }
}

function init()
{
    if (!initRole())
    {
        return;
    }
    if (m_role.creatorId > -1)//不可编辑
    {
        disabled();
    }
    initTab();
    initFrame();
    showTab(1);
    $("body").style.visibility = "visible";
}
function disabled()
{
    $("user_left").align = "center";
    $("func_left").align = "center";
    $("user_center").hide();
    $("func_center").hide();
    $("user_right").hide();
    $("func_right").hide();
//    var arrElem = document.getElementsByTagName("input");
//    for (var i = 0; i < arrElem.length; i++)
//    {
//        arrElem[i].disabled = true;
//    }
    $("role_btn").hide();
}
function initTab()
{
    tc = new CtnTabCtrl("tc");
    tc.create("role_tab");
    tc.append("基本信息", 1);
    tc.append("隶属用户", 2);
    tc.append("功能点权限", 3);
    tc.append("菜单权限", 4);
    tc.append("控制对象权限", 5);
    tc.setActive(0);
    tc.onTab = showTab;
}
function showTab(type)
{
    if  (type == m_curTabIdx)
    {
        return;
    }
    $("role_btn").show();
    switch (type)
    {
        case 1:
        {
            $("role_info").show();
            $("user_mng").hide();
            $("func_mng").hide();
            $("menu_mng").hide();
            $("obj_mng").hide();
            $("role_btn").hide();
            showRoleInfo();
            m_curTabIdx = 1;
        }break;
        case 2:
        {
            $("role_info").hide();
            $("user_mng").show();
            $("func_mng").hide();
            $("menu_mng").hide();
            $("obj_mng").hide();
            if (!m_arrUser)
            {
                m_arrUser = new Array();
                initListData("GetUserChilds", m_arrUser);
                showListData("GetRoleUsers", "hold_user", "spare_user", m_arrUser);
            }
            m_curTabIdx = 2;
        }break;
        case 3:
        {
            $("role_info").hide();
            $("user_mng").hide();
            $("func_mng").show();
            $("menu_mng").hide();
            $("obj_mng").hide();
            if (!m_arrFunc)
            {
                m_arrFunc = new Array();
                initListData("GetUserFuncs", m_arrFunc);
                showListData("GetRoleFuncs", "hold_func", "spare_func", m_arrFunc);
            }
            m_curTabIdx = 3;
        }break;
        case 4:
        {
            $("role_info").hide();
            $("user_mng").hide();
            $("func_mng").hide();
            $("menu_mng").show();
            $("obj_mng").hide();
            var elem = $("menu_mng");
            if (!elem.isdeal)
            {
                elem.isdeal = true;
                elem.src = "MngMenuTree.aspx#" + m_role.creatorId + "#1#" + m_role.id;
            }
            m_curTabIdx = 4;
        }break;
        case 5:
        {
            $("role_info").hide();
            $("user_mng").hide();
            $("func_mng").hide();
            $("menu_mng").hide();
            $("obj_mng").show();
            var elem = $("obj_mng");
            if (!elem.isdeal)
            {
                elem.isdeal = true;
                if (m_role.creatorId > -1)
                {
                    elem.src = "MngObj.aspx#0#1#" + m_role.id;
                }
                else
                {
                    elem.src = "MngObj.aspx#1#1#" + m_role.id;
                }
            }
            m_curTabIdx = 5;
        }break;
        default:break;
   }
}
function saveData()
{
    switch (m_curTabIdx)
    {
        case 1:
        {
        
        }break;
        case 2:
        {
            saveList('UpdateRoleUsers', 'hold_user');
        }break;
        case 3:
        {
            saveList('UpdateRoleFuncs', 'hold_func');
        }break;
        case 4:
        {
            $("menu_mng").contentWindow.saveMenu();
        }break;
        case 5:
        {
            $("obj_mng").contentWindow.saveObj();
        }break;
        default:break;
    }
}
function initRole()
{
    m_roleId = parseInt(window.location.href.split("?")[1]);
    var xmlDoc = CtnTrans.getXml("/main.ashx?_method=GetRoleInfo", "post", "RoleId=" + m_roleId);
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return false;
    }
    var root = CtnXML.getRootNode(xmlDoc);
    m_role.id = $XML(root.selectSingleNode("id"));
    m_role.name = $XML(root.selectSingleNode("name"));
    m_role.creator = $XML(root.selectSingleNode("creator"));
    m_role.creatorId = $XML(root.selectSingleNode("creatorid"));
    m_role.note = $XML(root.selectSingleNode("note"));
//    m_enable = $XML(root.selectSingleNode("enable"), null, "int");
    return true;
}

function showRoleInfo()
{
    $("role_id").innerHTML = m_role.id;
    $("role_name").innerHTML = m_role.name;
    $("role_creator").innerHTML = m_role.creator;
    $("role_note").innerHTML = m_role.note;
}

//功能点操作

function initListData(method, arrData)
{
    var xmlDoc = CtnTrans.getXml("/main.ashx?_Method=" + method, "post",  "&CreatorId=" + m_role.creatorId);
    if (!CtnError.validate(xmlDoc))
    {
        CtnError.show();
        return;
    }
    var rootNode = CtnXML.getRootNode(xmlDoc);
    if (rootNode)
    {
        var arrItem = rootNode.selectNodes("item");
        for (var i = 0; i < arrItem.length; i++)
        {
            var id = $XML(arrItem[i].selectSingleNode("id"));
            var name = $XML(arrItem[i].selectSingleNode("name"));
            arrData.push({id:id, name:name});
        } 
    }
}

function showListData(method, elemHoldId, elemSpareId, arrData)
{
    var text = CtnTrans.getText("/main.ashx", "post", "_method=" + method + "&RoleId=" + m_role.id);
    if (!CtnError.validate(text))
    {
        CtnError.show();
        return;
    }
    var arrId = new Array();
    if (text)
    {
        arrId = text.split(";");
    }
    var elemHold = $(elemHoldId);
    var elemSpare = $(elemSpareId);
    elemHold.innerHTML = "";
    elemSpare.innerHTML = "";
    j = 0;
    k = 0;
    for (var i = 0; i < arrData.length; i++)
    {
        var option = new Option(arrData[i].name, arrData[i].id);
        if (checkData(arrId, arrData[i].id))
        {
            elemHold.options[j++] = option;
        }
        else
        {
            elemSpare.options[k++] = option;
        }
    } 
}


function checkData(arrId, id)
{
    for (var i = 0; i < arrId.length; i++)
    {
        if (arrId[i] == id)
        {
            return true;
        }
    }
    return false;
}

function moveOptions(elemDestId, elemSrcId)
{
    var elemDest = $(elemDestId);
    var elemSrc = $(elemSrcId);
    var options = elemDest.options;
    for (var i = 0; i < options.length; i++)
    {
        if (options[i].selected == true)
        {
            options[i].selected = false;
        }
    }
    var options = elemSrc.options;
    var flag = false;
    for (var i = options.length - 1; i >= 0 ; i--)
    {
        if (options[i].selected)
        {
            elemDest.appendChild(options[i]);
            flag = true;
        }
    }
    if (!flag)
    {
        CtnDlg.warning("至少选择一项！");
    }
}

function saveList(method, elemId)
{
    var options = $(elemId).options;
    var str = "";
    for (var i = 0; i < options.length; i++)
    {
        str += options[i].value + ";";
    }
    if (str)
    {
        str = str.substr(0, str.length - 1);
    }
    var params = "_method=" + method
            + "&RoleId=" + m_role.id
            + "&Ids=" + str;
    var text = CtnTrans.getText("/main.ashx", "post", params);
    if (!CtnError.validate(text))
    {
        CtnError.show();
        return;
    }
    else
    {
        top.CtnDlg.msg("保存成功!");
    }
}